﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Drawing;

namespace GroupChat2013.Common
{
    /// <summary>
    /// windows 原始Api
    /// </summary>
    public class WinApi
    {
        public const int WM_COPYDATA = 0x004A;

        [DllImport("user32.dll")]
        /// <summary>
        /// 该函数设置由不同线程产生的窗口的显示状态;
        /// </summary>
        public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll")]
        /// <summary>
        /// 判断窗口是否已最小化
        /// </summary>
        public static extern bool IsIconic(IntPtr hWnd);

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        /// <summary>
        /// 查找窗体
        /// </summary>
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        /// <summary>
        /// 查找窗体增强版
        /// </summary>
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        /// <summary>
        /// 判断窗体是否可视
        /// </summary>
        public static extern bool IsWindowVisible(IntPtr hWnd);

        [DllImport("user32.dll")]
        /// <summary>
        /// 
        /// </summary>
        public static extern IntPtr GetWindowDC(IntPtr hWnd);

        [DllImport("user32.dll")]
        /// <summary>
        /// 
        /// </summary>
        public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("gdi32.dll")]
        /// <summary>
        /// 
        /// </summary>
        public static extern int GetClipBox(IntPtr hDC, ref Rectangle lpRect);

        [DllImport("user32.dll")]
        /// <summary>
        /// 获取活动窗体
        /// </summary>
        public static extern IntPtr GetActiveWindow();

        [DllImport("user32.dll", EntryPoint = "SetActiveWindow")]
        /// <summary>
        /// 设置活动窗体
        /// </summary>
        public static extern IntPtr SetActiveWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        /// <summary>
        /// 设置前端窗体
        /// </summary>
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        /// <summary>
        /// 发送消息
        /// </summary>
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        /// <summary>
        /// 发送消息
        /// </summary>
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        /// <summary>
        /// 发送消息
        /// </summary>
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam);

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        /// <summary>
        /// 发送消息
        /// </summary>
        public static extern int SendMessage(
        int hWnd, // handle to destination window
        int Msg, // message
        int wParam, // first message parameter
        ref COPYDATASTRUCT lParam // second message parameter
       );

        public const uint POWER_FORCE = 0x00001000u;
        public const uint POWER_STATE_RESET = 0x00800000u;        // reset state
        public const int SPI_SETWORKAREA = 47;
        public const int SPI_GETWORKAREA = 48;

        public const int SW_HIDE = 0x00;
        public const int SW_SHOW = 0x0001;//在窗口原来的位置以原来的尺寸激活和显示窗口。

        [DllImport("User32.dll", EntryPoint = "ShowWindow")]
        /// <summary>
        /// 显示窗体
        /// </summary>
        public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);

        [DllImport("User32.dll")]
        /// <summary>
        /// 
        /// </summary>
        public static extern int ShowCaret(IntPtr hwnd);

        [DllImport("User32.dll")]
        /// <summary>
        /// 闪烁窗体
        /// </summary>
        public static extern bool FlashWindow(IntPtr hWnd, bool bInvert);

        //闪烁窗体参数
        public const UInt32 FLASHW_STOP = 0;//停止闪动.系统将窗体恢复到初始状态.
        public const UInt32 FLASHW_CAPTION = 1;//闪动窗体的标题.
        public const UInt32 FLASHW_TRAY = 2;//闪动任务栏按钮
        public const UInt32 FLASHW_ALL = 3;//闪动窗体标题和任务栏按钮
        public const UInt32 FLASHW_TIMER = 4;//连续不停的闪动,直到此参数被设置为:FLASHW_STOP
        public const UInt32 FLASHW_TIMERNOFG = 12;

        [DllImport("User32.dll")] 
        /// <summary>
        /// 闪烁窗体增强版
        /// </summary>
        public static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

        [DllImport("user32.dll")]
        /// <summary>
        /// 设置鼠标位置
        /// </summary>
        public static extern bool GetCursorPos(out Point pt); 
    }

    /// <summary>
    /// 发送数据结构体
    /// </summary>
    public struct COPYDATASTRUCT
    {
        public IntPtr dwData;
        public int cbData;
        [MarshalAs(UnmanagedType.LPStr)]
        public string lpData;
    }

    [StructLayout(LayoutKind.Sequential)]
    /// <summary>
    /// 窗体闪烁结构体
    /// </summary>
    public struct FLASHWINFO
    {
        public UInt32 cbSize;
        public IntPtr hwnd;
        public UInt32 dwFlags;
        public UInt32 uCount;
        public UInt32 dwTimeout;
    }
}
